﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.LightSwitch;
namespace LightSwitchApplication
{
    public partial class Customer
    {
        partial void DOB_Validate(EntityValidationResultsBuilder results)
        {
            // results.AddPropertyError("<Error-Message>");
            if (this.DOB != null)
            {
                if (this.DOB > DateTime.Today)
                    results.AddPropertyError("Date of Birth cannot be in the future.");
                if (this.LastName != null && this.FirstName != null)
                {
                    if (this.DataWorkspace.ApplicationData.Customers.Where(customer => customer.FirstName == this.FirstName &&
                        customer.LastName == this.LastName && customer.DOB == this.DOB && customer.Id != this.Id).FirstOrDefault() != null)
                        results.AddPropertyError("Duplicated Customer: " + this.FullName + " (" + this.DOB.ToShortDateString() + ")");
                }
            }            
        }

        partial void FullName_Compute(ref string result)
        {
            // Set result to the desired field value
            result = this.LastName + ", " + this.FirstName;
        }

        partial void FirstName_Changed()
        {
            if (this.FirstName != null)
                this.FirstName = (this.FirstName.Substring(0, 1).ToUpper() + this.FirstName.Substring(1).ToLower()).Trim();
        }

        partial void LastName_Changed()
        {
            if (this.LastName != null)
                this.LastName = (this.LastName.Substring(0, 1).ToUpper() + this.LastName.Substring(1).ToLower()).Trim();
        }

        /*
        partial void FirstName_Changed()
        {
            if (this.FirstName == "test2")
                this.Address = "Yes!";
        }

        partial void Address_Changed()
        {
            if(this.Address == "Yes!")
            this.LastName = "!!";
        }*/
    }
}
